package bb.lanxing.chart.custom;

import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.buffer.BarBuffer;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.renderer.BarChartRenderer;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

public class RoundBarChartRenderer extends BarChartRenderer {
    private Path barPath;
    private RectF mBarShadowRectBuffer;
    private float roundRadius;

    public RoundBarChartRenderer(BarDataProvider barDataProvider, ChartAnimator chartAnimator, ViewPortHandler viewPortHandler) {
        super(barDataProvider, chartAnimator, viewPortHandler);
        this.mBarShadowRectBuffer = new RectF();
        this.barPath = new Path();
    }

    public void setRoundRadius(float f) {
        this.roundRadius = Utils.convertDpToPixel(f);
    }

    @Override
    protected void drawDataSet(Canvas canvas, IBarDataSet iBarDataSet, int i) {
        float f;
        Transformer transformer = this.mChart.getTransformer(iBarDataSet.getAxisDependency());
        this.mBarBorderPaint.setColor(iBarDataSet.getBarBorderColor());
        this.mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(iBarDataSet.getBarBorderWidth()));
        boolean z = iBarDataSet.getBarBorderWidth() > 0.0f;
        float phaseX = this.mAnimator.getPhaseX();
        float phaseY = this.mAnimator.getPhaseY();
        if (this.mChart.isDrawBarShadowEnabled()) {
            this.mShadowPaint.setColor(iBarDataSet.getBarShadowColor());
            float barWidth = this.mChart.getBarData().getBarWidth() / 2.0f;
            int min = Math.min((int) Math.ceil(iBarDataSet.getEntryCount() * phaseX), iBarDataSet.getEntryCount());
            int i2 = 0;
            while (i2 < min) {
                float x = ((BarEntry) iBarDataSet.getEntryForIndex(i2)).getX();
                this.mBarShadowRectBuffer.left = x - barWidth;
                this.mBarShadowRectBuffer.right = x + barWidth;
                transformer.rectValueToPixel(this.mBarShadowRectBuffer);
                if (!this.mViewPortHandler.isInBoundsLeft(this.mBarShadowRectBuffer.right)) {
                    f = barWidth;
                } else if (!this.mViewPortHandler.isInBoundsRight(this.mBarShadowRectBuffer.left)) {
                    break;
                } else {
                    this.mBarShadowRectBuffer.top = this.mViewPortHandler.contentTop();
                    this.mBarShadowRectBuffer.bottom = this.mViewPortHandler.contentBottom();
                    Path path = this.barPath;
                    float f2 = this.mBarShadowRectBuffer.left;
                    float f3 = this.mBarShadowRectBuffer.top;
                    float f4 = this.mBarShadowRectBuffer.right;
                    float f5 = this.mBarShadowRectBuffer.bottom;
                    f = barWidth;
                    float f6 = this.roundRadius;
                    setRoundedRect(path, f2, f3, f4, f5, f6, f6, true);
                    canvas.drawPath(this.barPath, this.mShadowPaint);
                }
                i2++;
                barWidth = f;
            }
        }
        BarBuffer barBuffer = this.mBarBuffers[i];
        barBuffer.setPhases(phaseX, phaseY);
        barBuffer.setDataSet(i);
        barBuffer.setInverted(this.mChart.isInverted(iBarDataSet.getAxisDependency()));
        barBuffer.setBarWidth(this.mChart.getBarData().getBarWidth());
        barBuffer.feed(iBarDataSet);
        transformer.pointValuesToPixel(barBuffer.buffer);
        boolean z2 = iBarDataSet.getColors().size() == 1;
        if (z2) {
            this.mRenderPaint.setColor(iBarDataSet.getColor());
        }
        for (int i3 = 0; i3 < barBuffer.size(); i3 += 4) {
            int i4 = i3 + 2;
            if (this.mViewPortHandler.isInBoundsLeft(barBuffer.buffer[i4])) {
                if (!this.mViewPortHandler.isInBoundsRight(barBuffer.buffer[i3])) {
                    return;
                }
                if (!z2) {
                    this.mRenderPaint.setColor(iBarDataSet.getColor(i3 / 4));
                }
                Path path2 = this.barPath;
                float f7 = barBuffer.buffer[i3];
                float f8 = barBuffer.buffer[i3 + 1];
                float f9 = barBuffer.buffer[i4];
                float f10 = barBuffer.buffer[i3 + 3];
                float f11 = this.roundRadius;
                setRoundedRect(path2, f7, f8, f9, f10, f11, f11, true);
                canvas.drawPath(this.barPath, this.mRenderPaint);
                if (z) {
                    canvas.drawPath(this.barPath, this.mBarBorderPaint);
                }
            }
        }
    }

    private static void setRoundedRect(Path path, float f, float f2, float f3, float f4, float f5, float f6, boolean z) {
        path.reset();
        if (f5 < 0.0f) {
            f5 = 0.0f;
        }
        if (f6 < 0.0f) {
            f6 = 0.0f;
        }
        float f7 = f3 - f;
        float f8 = f4 - f2;
        float f9 = f7 / 2.0f;
        if (f5 > f9) {
            f5 = f9;
        }
        float f10 = f8 / 2.0f;
        if (f6 > f10) {
            f6 = f10;
        }
        float f11 = f7 - (f5 * 2.0f);
        float f12 = f8 - (2.0f * f6);
        path.moveTo(f3, f2 + f6);
        float f13 = -f6;
        float f14 = -f5;
        path.rQuadTo(0.0f, f13, f14, f13);
        path.rLineTo(-f11, 0.0f);
        path.rQuadTo(f14, 0.0f, f14, f6);
        path.rLineTo(0.0f, f12);
        if (z) {
            path.rLineTo(0.0f, f6);
            path.rLineTo(f7, 0.0f);
            path.rLineTo(0.0f, f13);
        } else {
            path.rQuadTo(0.0f, f6, f5, f6);
            path.rLineTo(f11, 0.0f);
            path.rQuadTo(f5, 0.0f, f5, f13);
        }
        path.rLineTo(0.0f, -f12);
        path.close();
    }
}
